//
//  EmoticonPackage.swift
//  VVbo
//
//  Created by Mr.Run on 2017/9/7.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {
    
    lazy var emoticons = [Emoticon]()
    
    var numberOfPages: Int {
        return (emoticons.count - 1)/20+1
    }
    
    func emoticon(page: Int) -> [Emoticon] {
        
        let count = 20
        let location = page*count
        var length = count
        
        if location + length > emoticons.count{
            length = emoticons.count - location
        }
        
        let range = NSRange(location: location, length: length)
        
        let subArr = (emoticons as NSArray).subarray(with: range)
        
        return subArr as! [Emoticon]
    }
    
    var groupName: String?
    var bgImageName: String?
    
    var directory: String? {
        didSet{
            guard let directory = directory,
                let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil),
                let bundle = Bundle(path: path),
                let infoPath = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: directory),
                let array = NSArray(contentsOfFile: infoPath) as? [[String: String]],
                let models = NSArray.yy_modelArray(with: Emoticon.self, json: array) as? [Emoticon]
                else {
                return
            }
            
            for m in models {
                m.directory = directory
            }
            
            emoticons += models
            
        }
    }
    
    override var description: String{
        return yy_modelDescription()
    }
    
}
